package com.jcfk.eam.controller.mould;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.security.annotation.AllowAnonymous;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.domain.dto.main.EamAssetInfoDTO;
import com.jcfk.eam.domain.dto.mould.EamMouldTaskInfoDTO;
import com.jcfk.eam.domain.dto.mould.EamMouldTaskInfoDetailDTO;
import com.jcfk.eam.domain.query.mould.EamMouldTaskInfoQuery;
import com.jcfk.eam.service.mould.EamMouldTaskInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 模具任务表
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2025-07-17
 */
@Api(tags = {"模具任务表管理"})
@RestController
@RequestMapping("eammouldtaskinfo")
public class EamMouldTaskInfoController {
    @Autowired
    private EamMouldTaskInfoService eamMouldTaskInfoService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamMouldTaskInfoQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询模具任务表")
    @PostMapping("/queryByPage")
    public RetPage<EamMouldTaskInfoDTO> queryByPage(@RequestBody ReqPageParam<EamMouldTaskInfoQuery> requestParam) {
        DataPage<EamMouldTaskInfoDTO> page = new DataPage<EamMouldTaskInfoDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                EamMouldTaskInfoDTO.class);
        page = eamMouldTaskInfoService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamMouldTaskInfoQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取模具任务表列表", notes = "获取模具任务表列表", httpMethod = "POST")
    @PostMapping("getDetails")
    public RetResult<List<EamMouldTaskInfoDetailDTO>> getDetails(@RequestBody EamMouldTaskInfoQuery query) {

        List<EamMouldTaskInfoDetailDTO> list = eamMouldTaskInfoService.getDetails(query);

        return RetResult.ok(list);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamMouldTaskInfoQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取模具任务表列表", notes = "获取模具任务表列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<EamMouldTaskInfoDTO>> getList(@RequestBody EamMouldTaskInfoQuery query) {

        List<EamMouldTaskInfoDTO> list = eamMouldTaskInfoService.getList(query);

        return RetResult.ok(list);
    }


    /**
     * 模具任务表
     * 根据id 查询详情
     */
    @ApiOperation("查询模具任务表")
    @GetMapping("/get/{id}")
    public RetResult<EamMouldTaskInfoDTO> get(@PathVariable("id") String id) {
        EamMouldTaskInfoDTO item = eamMouldTaskInfoService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理模具任务表
     */
    @ApiOperation("保存模具任务表")
    @PostMapping("/save")
    @RequestLogHeader(modular = "模具任务表管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamMouldTaskInfoDTO eamMouldTaskInfo) {
        // 数据校验
//        RetResult ret = eamMouldTaskInfoService.isValid(eamMouldTaskInfo);
//        if (!ret.getCode().equals(RetResult.SUCCESS)) {
//            return ret;
//        }

        if (StrUtil.isEmpty(eamMouldTaskInfo.getId())) {
            // 新增
            eamMouldTaskInfoService.insert(eamMouldTaskInfo);
        } else {
            // 更新
            eamMouldTaskInfoService.update(eamMouldTaskInfo);
        }
        return RetResult.ok();

    }

    /**
     * 添加/更新管理模具任务表
     */
    @ApiOperation("模具生产任务同步创建接口")
    @PostMapping("/syncTaskAdd")
    @AllowAnonymous
    @RequestLogHeader(modular = "模具任务表管理", businessType = "保存")
    public RetResult<?> syncTask(@RequestBody @Valid List<EamMouldTaskInfoDTO> eamMouldTaskInfoList) {
        for (EamMouldTaskInfoDTO eamMouldTaskInfo : eamMouldTaskInfoList) {
            // 数据校验
            RetResult ret = eamMouldTaskInfoService.isValid(eamMouldTaskInfo);
            if (!ret.getCode().equals(RetResult.SUCCESS)) {
                return ret;
            }
            // 新增
            eamMouldTaskInfo.setTaskStatus(0);
            eamMouldTaskInfoService.insert(eamMouldTaskInfo);
        }
        return RetResult.ok();
    }

    /**
     * 添加/更新管理模具任务表
     */
    @ApiOperation("模具生产任务同步撤回接口")
    @PostMapping("/syncTaskDel")
    @RequestLogHeader(modular = "模具任务表管理", businessType = "保存")
    public RetResult<?> syncTaskDetele(@RequestBody @Valid List<EamMouldTaskInfoDTO> eamMouldTaskInfoList) {
        // 删除
        for (EamMouldTaskInfoDTO eamMouldTaskInfo : eamMouldTaskInfoList) {
            eamMouldTaskInfoService.logicDeletePlan(eamMouldTaskInfo);
        }
        return RetResult.ok();

    }


    /**
     * 批量删除模具任务表
     */
    @ApiOperation("批量删除模具任务表")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "模具任务表管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamMouldTaskInfoDTO eamMouldTaskInfo) {
        return RetResult.ok(eamMouldTaskInfoService.logicDeleteList(eamMouldTaskInfo));
    }


    /**
     * 删除模具任务表
     */
    @ApiOperation("删除模具任务表")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "模具任务表管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamMouldTaskInfoService.deleteItem(id);
        return RetResult.ok();
    }

    /**
     * 出库提交
     */
    @ApiOperation("模具出库提交接口")
    @PostMapping("/mouldSave")
    @RequestLogHeader(modular = "模具提交", businessType = "保存")
    public RetResult<?> mouldSave(@RequestBody EamMouldTaskInfoDTO EamMouldTaskInfoDTO) {
        if (eamMouldTaskInfoService.checkMouldSave(EamMouldTaskInfoDTO)) {
            return RetResult.ok(eamMouldTaskInfoService.mouldSave(EamMouldTaskInfoDTO));
        }
        return RetResult.fail("只有正常/生产且启用的在库模具才可以出库");
    }

    /**
     * 出库维修提交
     */
    @ApiOperation("模具出库维修提交接口")
    @PostMapping("/mouldSaveMaintenance")
    @RequestLogHeader(modular = "模具出库维修", businessType = "保存")
    public RetResult<?> mouldSaveMaintenance(@RequestBody EamMouldTaskInfoDTO EamMouldTaskInfoDTO) {
        eamMouldTaskInfoService.mouldSaveMaintenance(EamMouldTaskInfoDTO);
        return RetResult.ok();
    }

    /**
     * 出库提交
     */
    @ApiOperation("模具入库提交接口")
    @PostMapping("/mouldSaveStore")
    @RequestLogHeader(modular = "模具提交", businessType = "保存")
    public RetResult<?> mouldSaveStore(@RequestBody EamAssetInfoDTO eamAssetInfoDTO) {
        if (eamMouldTaskInfoService.checkMouldSaveStore(eamAssetInfoDTO)) {
            return RetResult.ok(eamMouldTaskInfoService.mouldSaveStore(eamAssetInfoDTO));
        }
        return RetResult.fail("不在库的模具才可以入库");
    }
}
